/**
 * Created by lizhihang
 */

'use strict';
var express = require('express');
var router = express.Router();
var logger = require('../logger');
var superagent = require('superagent');
var jwt = require('jsonwebtoken');
var mongo = require('mongoskin');
const uuidV4 = require('uuid/v4');
var db = mongo.db("mongodb://localhost:27017/monents");
 db.bind('monents');


//测试分页接口,省略了token验证
function testgetAllMonents(req, res, next) {
				var token = req.body.token
				if(token){
            //尝试是否可以解析token，如果是格式正确的token，允许查找数据库，并且继续搜索教务系统
            try {
                let decoded = jwt.verify(token, 'thefirstminappofvtmer');
                console.log(decoded)
                let openid = decoded.openid;
                //查找数据库中是否已经绑定
								var db1 = mongo.db("mongodb://localhost:27017/weGDUT");
								db1.bind('weGDUT');
                var result = new Promise((resolve, reject) => {
                    db1.weGDUT.find({ openid: openid }).toArray(function (err, items) {
                        resolve(items)
                    })
                })
                var users;
                result.then(function (value) {
                    users = value[0]
                    if (!users) {
                        res.send({ code: 505, message: 'No this user' })
                    }else {
												//flag时间戳 上一条动态的时间戳，如果没有则为0，send最新的20条
												var flag = req.body.time
																if(!flag){
																				flag = 0
																}
																console.log("test:"+flag)
												if(!flag){
														var monentsRes = new Promise((resolve, reject) => {
																db.monents.find().limit(20).sort( { created_time: -1 } ).toArray(function (err, items) {
																resolve(items)
																})
														})
														monentsRes.then(function (value) {
																res.send({ code: 200, message: 'ok', data:value })
																 }), function (err) {
																			 res.send({ code: 501, message: 'mongo DB error' })
																		 }
												}else {
																//字符串转化为数字整型,否则查询为空
																flag = +flag
																console.log('flag:'+flag)
																var nextPage = new Promise((resolve,reject)=>{
																				//取小于flag的20条动态
																				db.monents.find({created_time: {$lt: flag }}).limit(20).sort( { created_time: -1 } ).toArray(function (err, items) {
																				resolve(items)
																				})
																})
																nextPage.then(function(value){
																				res.send({ code: 200, message: 'nextPage ok', data:value })
																				}), function (err) {
																								res.send({ code: 501, message: 'mongo DB error' })
																				}
																	}
													}
									})
								} catch (err) {
												res.send({ code: 401, message: 'token invid' })
								}
				 } else {
								res.send({ code: 402, message: 'token or content is required' })
    }
}

function getPersonMonents(req, res, next) {
        var token = req.body.token
        console.log('allmonent:'+token)
        if(token){
            //尝试是否可以解析token，如果是格式正确的token，允许查找数据库，并且继续搜索教务系统
            try {
                let decoded = jwt.verify(token, 'thefirstminappofvtmer');
                console.log(decoded)
                let openid = decoded.openid;
                //查找数据库中是否已经绑定

                var result = new Promise((resolve, reject) => {
                    db.weGDUT.find({ openid: openid }).toArray(function (err, items) {
                        resolve(items)
                    })
                })
                var users;
                result.then(function (value) {
                    users = value[0]
                    if (!users) {
                        res.send({ code: 505, message: 'No this user' })
                    }else {

                        var db = mongo.db("mongodb://localhost:27017/monents");
                        db.bind('monents');
                        var monentsRes = new Promise((resolve, reject) => {
                            db.monents.find({openid:users.openid}).toArray(function (err, items) {
                                resolve(items)
                            })
                        })
                        monentsRes.then(function (value) {
                            res.send({ code: 200, message: 'ok', data:value })
                        }), function (err) {
                            res.send({ code: 501, message: 'mongo DB error' })
                        }

                    }
                })
            } catch (err) {
                res.send({ code: 401, message: 'token invid' })
            }
        }else{
            res.send({ code: 500, message: 'token is required' })
        }
    } 	
function getValidateImg(req, res, next){
				const url = 'http://jxfw.gdut.edu.cn/yzm?d=1499920703672'



}
//module.exports = { send, getAllMonents, sendComment, like, detail ,del, getPersonMonents};
module.exports = { testgetAllMonents };
